/*
 * @Author: LAB
 * @Date: 2021-03-25 14:07:45
 * @Description: 常用正则表达式
 * @FilePath: \Clinic-Web\clientapp\src\utils\validate.ts
 */
// 正则类
export interface Pattern {
    msg: string;
    reg: RegExp;
    name: string
}
// 验证类 包含常用的正则表达式
export class Validate {
    constructor(pattern?: Pattern | Pattern[]) {
        let patterns = this.popularPatterns()
        if (pattern instanceof Array) {
            patterns = patterns.concat(pattern)
        } else if (pattern) {
            patterns.push(pattern)
        }
        this.setPattern(patterns)
    }
    private patterns: Map<string, Pattern> = new Map();
    // 添加Pattern
    setPattern(pattern: Pattern | Pattern[]) {
        if (pattern instanceof Array) {
            pattern.map(item => {
                this.patterns.set(item.name, item)
            })
        } else {
            this.patterns.set(pattern.name, pattern);
        }
    }
    // 获取
    getPattern(name: string): Pattern {
        return this.patterns.get(name);
    }
    // 返回patterns
    get Patterns() {
        return this.patterns;
    }
    // 常用正则
    private popularPatterns(): Pattern[] {
        return [
            {
                msg: "请输入正确的手机号",
                reg: /^1[0-9]{10}$/,
                name: "mobile",
            },
            {
                msg: "输入不能为空",
                reg: /\S/,
                name: "blank",
            },
            {
                msg: "请输入大小写英文字母",
                reg: /^[a-zA-Z]+$/,
                name: "words",
            },
            {
                msg: "请输入正确时间格式YYYY-MM-DD",
                reg: /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/,
                name: "date",
            },
            {
                msg: "请输入正确时间格式YYYY-MM-DD hh:mm:ss",
                reg: /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/,
                name: "dateTime",
            },
            {
                msg: "请输入整数",
                reg: /^[-+]?\d*$/,
                name: "int",
            },
            {
                msg: "请输入中文",
                reg: /^[\u0391-\uFFE5]+$/,
                name: "chinese",
            },
            {
                msg: "请输入邮箱",
                reg: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
                name: "email",
            },
            {
                msg: "请输入身份证号",
                reg: /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/,
                name: "idcard",
            },
            {
                msg: "格式有误,最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符",
                reg: /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/,
                name: "password",
            },
            {
                msg: "仅允许输入字母,数字,下划线和减号",
                reg: /^[a-zA-Z0-9_-]+$/,
                name: "powerEnglish",
            },
            {
                msg: "仅允许输入中英文和数字",
                reg: /^[a-zA-Z0-9\u4E00-\u9FA5]+$/,
                name: "chineseEnglish",
            },
            {
                msg: "仅允许输入英文和数字",
                reg: /^[a-zA-Z0-9]+$/,
                name: "english",
            },
            {
                msg: "请输入普通文本",
                reg: /^[a-zA-Z0-9\u4E00-\u9FA5]+$/,
                name: "text",
            },
        ]
    }
}
export default Validate